********************************************************************************
******"Macroprudential Regulation, Quantitative Easing, and Bank Lending"*******
********************************************************************************
global y "lncdisbur_w199"
global ec4 "dT1e14 dT2e14 dT3e14 dT4e14 dT5e14 dT7e14 dT8e14 dT9e14 dT10e14 dT11e14 dT12e14"
global ed4top "dT1dtop66e14 dT2dtop66e14 dT3dtop66e14 dT4dtop66e14 dT5dtop66e14 dT7dtop66e14 dT8dtop66e14 dT9dtop66e14 dT10dtop66e14 dT11dtop66e14 dT12dtop66e14"
global ed4mid "dT1dmid6633e14 dT2dmid6633e14 dT3dmid6633e14 dT4dmid6633e14 dT5dmid6633e14 dT7dmid6633e14 dT8dmid6633e14 dT9dmid6633e14 dT10dmid6633e14 dT11dmid6633e14 dT12dmid6633e14"
global controls "lncrefin dT*exlnassets dT*excashas"
global regcommands ", pool(5) compact absorb(bankid borrowerid#time) vce(cluster bankid)"

**# Bookmark (input)
clear *
import sas ENTE_SEGN CENSITO_CRITTOG TIME DISBUR COM COMUNE_DIP using $inputdirectory/bloans2019, case(lower)
run RO_creditregister tm(2019m2) tm(2020m2) 0
keep bankid borrowerid time disbur

**# Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_gru2019, keepus(gru)
keep if _merge!=2
drop _merge
replace gru=bankid if gru==.
drop bankid
rename gru bankid
collapse (sum) disbur, by(bankid borrowerid time)

**# Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2019m8, keepus(e14 dtop66e14 dmid6633e14 exlnassets excashas ema4 ext1r extltroas exnetinterbankas excbabserd0 excbabs4 excspp4 exunusallow)
keep if _merge==3
drop _merge

**# Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_refinm2019, keepus(lncrefin)
keep if _merge!=2
drop _merge

egen id=group(bankid borrowerid)
xtset id time
gen lncdisbur=100*(ln(disbur)-ln(L.disbur))
keep if time>=tm(2019m3)&time<=tm(2020m2)
winsor2 lncdisbur if lncdisbur!=., suffix(_w199) cuts(1 99)

compress
run RO_tdummies 2019 12
run RO_tdintera 12 "e14 dtop66e14 dmid6633e14 exlnassets excashas ema4 ext1r extltroas exnetinterbankas excbabserd0 excbabs4 excspp4 exunusallow"

**# Bookmark (output - IIB)
est clear 
cd $outputdirectory
reghdfe $y $ec4 $controls dT*ema4 $regcommands
est save TABIIB_C12345678
reghdfe $y $ec4 $controls dT*ext1r $regcommands
est save TABIIB_C12345678, append
reghdfe $y $ec4 $controls dT*extltroas $regcommands
est save TABIIB_C12345678, append
reghdfe $y $ec4 $controls dT*exnetinterbankas $regcommands
est save TABIIB_C12345678, append
reghdfe $y $ec4 $controls dT*excbabserd0 $regcommands
est save TABIIB_C12345678, append
reghdfe $y $ec4 $controls dT*excbabs4 $regcommands
est save TABIIB_C12345678, append
reghdfe $y $ec4 $controls dT*excspp4 $regcommands
est save TABIIB_C12345678, append
reghdfe $y $ec4 $controls dT*exunusallow $regcommands
est save TABIIB_C12345678, append

**# Bookmark (output - IIC)
reghdfe $y $ed4top $ed4mid $controls dT*ema4 $regcommands
est save TABIIC_C12345678,
reghdfe $y $ed4top $ed4mid $controls dT*ext1r $regcommands
est save TABIIC_C12345678, append
reghdfe $y $ed4top $ed4mid $controls dT*extltroas $regcommands
est save TABIIC_C12345678, append
reghdfe $y $ed4top $ed4mid $controls dT*exnetinterbankas $regcommands
est save TABIIC_C12345678, append
reghdfe $y $ed4top $ed4mid $controls dT*excbabserd0 $regcommands
est save TABIIC_C12345678, append
reghdfe $y $ed4top $ed4mid $controls dT*excbabs4 $regcommands
est save TABIIC_C12345678, append
reghdfe $y $ed4top $ed4mid $controls dT*excspp4 $regcommands
est save TABIIC_C12345678, append
reghdfe $y $ed4top $ed4mid $controls dT*exunusallow $regcommands
est save TABIIC_C12345678, append
cd ..
